package com.mengmablog.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mengmablog.blog.DTO.GetCommentsDTO;
import com.mengmablog.blog.commons.ServerResponse;
import com.mengmablog.blog.dao.ArticlesMapper;
import com.mengmablog.blog.dao.CommentsMapper;
import com.mengmablog.blog.entity.Articles;
import com.mengmablog.blog.entity.Comments;
import com.mengmablog.blog.service.port.ICommentsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentsServiceImpl implements ICommentsService {

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private ArticlesMapper articlesMapper;

    @Override
    public ServerResponse comment(String articleId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Comments> commentsList = commentsMapper.findByArticleId(articleId);
        PageInfo pageInfo = new PageInfo(commentsList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse comment(Integer articleId, Integer pId, String content, String name, String website, String email) {
        int i = commentsMapper.insertByComment(articleId, pId, content, name, website, email);
        if (i > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

    @Override
    public ServerResponse comment_commentId_assess(String commentId, String assess) {
        if (assess.equals("AGREE") || assess.equals("DISAGREE")) {
            commentsMapper.updateByCommentIdAssess(commentId, assess);
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse admin_comment(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Comments> commentsList = commentsMapper.findByAllComment();
        PageInfo pageInfo = new PageInfo(commentsList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse comment_count() {
        List<Comments> commentsList = commentsMapper.findByAllComment();
        return ServerResponse.createBySuccess(commentsList.size());
    }

    @Override
    public ServerResponse comment_id(Integer id) {
        Comments comments = commentsMapper.selectByPrimaryKey(id);
        GetCommentsDTO getCommentsDTO = new GetCommentsDTO();

        BeanUtils.copyProperties(comments, getCommentsDTO);

        Articles articles = articlesMapper.selectByPrimaryKey(comments.getArticleId());
        Comments pComments = commentsMapper.selectByPrimaryKey(comments.getpid());

        getCommentsDTO.setArticle(articles);
        getCommentsDTO.setPcomment(pComments);
        return ServerResponse.createBySuccess(getCommentsDTO);
    }

    @Override
    public ServerResponse delete(Integer id) {
        int i = commentsMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("删除评论失败");
    }

}
